package com.example.kai.uno.uno;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

import com.example.kai.uno.R;

import java.util.Random;

public class Person {

	int[] pokes;

	boolean[] pokesFlag;

	int top, left;

	int id;
	int Num;
	Desk desk;

	Card card;
	DDZ ddz;

	int paintDir = PokeType.dirV;
	Bitmap pokeImage;

	private Person last;
	private Person next;

	public Person(int[] pokes, int top, int left, int paintDir, int id,
			Desk desk, DDZ ddz) {
		this.desk = desk;
		this.id = id;
		this.pokes = pokes;
		pokesFlag = new boolean[pokes.length];
		this.setPos(left, top);
		this.paintDir = paintDir;
		this.ddz = ddz;
		pokeImage = BitmapFactory.decodeResource(ddz.getResources(),
				R.drawable.uno);
//		this.Num=num;
	}

	public int getNum() {
		return Num;
	}

	public void setNum(int num) {
		Num = num;
	}

	// ����������¼ҹ�ϵ
	public void setPosition(Person last, Person next) {
		this.last = last;
		this.next = next;
	}

	public void setPos(int l, int t) {
		this.left = l;
		this.top = t;
	}

	// ����������е���
	public void paint(Canvas canvas) {
		Rect src = new Rect();
		Rect des = new Rect();
		for (int i = 0; i < pokes.length; i++) {
			int row = Poke.getImageRow(pokes[i]);
			int col = Poke.getImageCol(pokes[i]);

			if (paintDir == PokeType.dirV) {
				row = 4;
				col = 4;
				src.set(col * 35, row * 52, col * 35 + 35, row * 52 + 52);
				des.set(left, top + i * 8, left + 35, top + 52 + i * 8);
			} else {
				// ��ǰ��һ���
				row = Poke.getImageRow(pokes[i]);
				col = Poke.getImageCol(pokes[i]);
				int select = 0;
				if (pokesFlag[i]) {
					select = 10;
				}
				src.set(col * 35, row * 52, col * 35 + 35, row * 52 + 52);
				des.set(left + i * 13, top - select, left + 35 + i * 13, top
						- select + 52);

			}
			canvas.drawBitmap(pokeImage, src, des, null);
		}

	}
//	public void add4(Card card){
//		if (card.poke==53){
//			pokes[ddz]
//		}
//	}
	public Card colorselect(int num){
		int pokeWanted=-1;
		pokeWanted=Poke.colorselect(num);
		Card thiscard = new Card(pokeWanted, pokeImage, id);

		Desk.currentCard = thiscard;
		this.card = thiscard;
		return thiscard;
	}
	public Card chupaiAI(Card a) {
		int pokeWanted=-1;
		if (a==null){
			pokeWanted=Poke.outCardByItsself(pokes);
		}
		else {
			pokeWanted=Poke.findTheRightCard(a,pokes);
		}
		if (pokeWanted==-1){
			return null;
		}
		int num = 0;

			for (int j = 0; j < pokes.length; j++) {
				if (pokes[j] == pokeWanted) {
					pokes[j] = -1;
					num++;
					break;
				}
			}

		int[] newpokes = new int[0];
		if (pokes.length - 1 > 0) {
			newpokes = new int[pokes.length - 1];
		}
		int j = 0;
		for (int i = 0; i < pokes.length-1; i++) {
			if (pokes[i] != -1) {
				newpokes[j] = pokes[i];
				j++;

			}
		}
		this.pokes = newpokes;
		Card thiscard = new Card(pokeWanted, pokeImage, id);

		Desk.currentCard = thiscard;
		this.card = thiscard;
		return thiscard;
	}

	public Card chupai(Card card) {
		int count = 0;
		for (int i = 0; i < pokes.length; i++) {
			if (pokesFlag[i]) {
				count++;
			}
		}
		int cardPokes = -1;
		int j = 0;
		for (int i = 0; i < pokes.length; i++) {
			if (pokesFlag[i]) {
				cardPokes = pokes[i];
				j++;
			}
		}
		Card thiscard = new Card(cardPokes, pokeImage, id);
	//	if (card == null) {
			desk.currentCard = thiscard;
			this.card = thiscard;

			int[] newPokes = new int[pokes.length - count];
			int ni = 0;
			for (int i = 0; i < pokes.length; i++) {
				if (!pokesFlag[i]) {
					newPokes[ni] = pokes[i];
					ni++;
				}
			}
			this.pokes = newPokes;
			this.pokesFlag = new boolean[pokes.length];

			return thiscard;
	//	}
//		else {
//
//			if (Poke.compare(thiscard, card)) {//////////////////？？？？？？？？？？？？
//				desk.currentCard = thiscard;
//				this.card = thiscard;
//
//				int[] newPokes = new int[pokes.length - count];
//				int ni = 0;
//				for (int i = 0; i < pokes.length; i++) {
//					if (!pokesFlag[i]) {
//						newPokes[ni] = pokes[i];
//						ni++;
//					}
//				}
//				this.pokes = newPokes;
//				this.pokesFlag = new boolean[pokes.length];
//
//				return thiscard;
//			} else {
//				Poke.show("��̫С��", 100);
//				return null;
//			}
		//}

	}

	// ������Լ�����ʱ���������¼��Ĵ���
	public void onTuch(View v, MotionEvent event) {

		int x = (int) event.getX();
		int y = (int) event.getY();

		for (int i = pokes.length -1; i >= 0; i--) {
			// �ж��������Ʊ�ѡ�У����ñ�־
			if (Poke.inRect(x, y, left + i * 13, top - (pokesFlag[i] ? 10 : 0),
					35, 52)) {
				pokesFlag[i] = !pokesFlag[i];
				break;
			}
		}
	}
}
